package com.codoon.unova.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class CircleView extends View {

	public static final double PI = 3.14159;

	private float mRadius = 61;
	private int mSteps = 0;
	private int mTarget = 10000;
	private int mOuterColor = 0;
	private int mInnerColor = 0;
	private int mOverTargetColor = 0;
	private int mBackgroundColor = 0;

	private int mOuterStrokeWidth = 40;
	private int mInnerStrokeWidth = 10;
	private Point mPoint;
	private boolean isEnd = true;

	public CircleView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public CircleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public CircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void setmRadius(float mRadius) {
		this.mRadius = mRadius;
	}

	public void setmSteps(int mSteps) {
		this.mSteps = mSteps;
	}

	public void setmTarget(int mTarget) {
		this.mTarget = mTarget;
	}

	public void setmOuterColor(int mOuterColor) {
		this.mOuterColor = mOuterColor;
	}

	public void setmInnerColor(int mInnerColor) {
		this.mInnerColor = mInnerColor;
	}

	public void setmOverTargetColor(int mOverTargetColor) {
		this.mOverTargetColor = mOverTargetColor;
	}

	public void setmBackgroundColor(int mBackgroundColor) {
		this.mBackgroundColor = mBackgroundColor;
	}

	public void setmOuterStrokeWidth(int mOuterStrokeWidth) {
		this.mOuterStrokeWidth = mOuterStrokeWidth;
	}

	public void setmInnerStrokeWidth(int mInnerStrokeWidth) {
		this.mInnerStrokeWidth = mInnerStrokeWidth;
	}

	public void setmPoint(Point mPoint) {
		this.mPoint = mPoint;
	}

	public void setEnd(boolean isEnd) {
		this.isEnd = isEnd;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		// 判定坐标点，如果坐标为空，默认设置为屏幕的中央
		if (mPoint == null) {
			WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			int width = display.getWidth();
			int height = display.getHeight() + 20;
			mPoint = new Point(width, height);
		}

		// LogUtil.info(" width=" + mPoint.x + " ,height=" + mPoint.y);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		// 绘制内圆
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(mInnerColor);
		paint.setStrokeWidth(mInnerStrokeWidth);
		canvas.drawCircle(mPoint.x / 2, mPoint.y / 2, mRadius, paint);

		/* 绘制外圆（进度圆） */
		paint.setColor(mOuterColor);
		paint.setStrokeWidth(mOuterStrokeWidth);
		RectF f = new RectF(mPoint.x / 2 - mRadius, mPoint.y / 2 - mRadius, mPoint.x / 2 + mRadius, mPoint.y / 2
				+ mRadius);
		// 弧形角度
		float sweepAngle = 0;
		if (mSteps >= mTarget) {
			paint.setColor(mOverTargetColor);
			sweepAngle = 360;
		} else {
			if (mTarget == 0) {
				sweepAngle = 0;
			} else {
				sweepAngle = ((mSteps * 1f) / mTarget) * 360;
			}
		}
		canvas.drawArc(f, 90, sweepAngle, false, paint);
		paint.setStyle(Paint.Style.FILL);
		if (isEnd) {
			float x = 0, y = 0;
			if (sweepAngle <= 90) {
				x = (float) (mRadius * Math.sin(sweepAngle * 1.0 / 180 * PI));
				y = (float) (mRadius * Math.cos(sweepAngle * 1.0 / 180 * PI));
				canvas.drawCircle((mPoint.x / 2) - Math.abs(x), (mPoint.y / 2) + Math.abs(y), 3, paint);
			} else if (sweepAngle > 90 && sweepAngle < 180) {
				x = (float) (mRadius * Math.sin(sweepAngle * 1.0 / 180 * PI));
				y = (float) (mRadius * Math.cos(sweepAngle * 1.0 / 180 * PI));
				canvas.drawCircle((mPoint.x / 2) - Math.abs(x), (mPoint.y / 2) - Math.abs(y), 3, paint);
			} else if (sweepAngle > 180 && sweepAngle < 270) {
				x = (float) (mRadius * Math.sin(sweepAngle * 1.0 / 180 * PI));
				y = (float) (mRadius * Math.cos(sweepAngle * 1.0 / 180 * PI));
				canvas.drawCircle((mPoint.x / 2) + Math.abs(x), (mPoint.y / 2) - Math.abs(y), 3, paint);
			} else {
				x = (float) (mRadius * Math.sin(sweepAngle * 1.0 / 180 * PI));
				y = (float) (mRadius * Math.cos(sweepAngle * 1.0 / 180 * PI));
				canvas.drawCircle((mPoint.x / 2) + Math.abs(x), (mPoint.y / 2) + Math.abs(y), 3, paint);
			}
		}

	}

	/**
	 * 
	 * Function:更新步数，重绘进度环
	 * 
	 * @param steps
	 *            :步数
	 */
	public void updateSteps(int steps) {
		this.mSteps = steps;
		invalidate();
	}
}
